package com.zzyl.nursing.controller;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.DeviceShadowDataVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备Controller
 *
 * @author FuJiaWei
 * @date 2025-05-30
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备列表")
    public TableDataInfo<List<Device>> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 从物联网平台同步产品列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:sync')")
    @ApiOperation(value = "从物联网平台同步产品列表")
    @PostMapping("/syncProductList")
    public R syncProductList() {
        deviceService.syncProductList();
        return R.ok();
    }

    /**
     * 查询所有产品列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @ApiOperation(value = "获取所有设备")
    @GetMapping("/allProduct")
    public R<List<ProductVo>> findAll() {
        List<ProductVo> productVos = deviceService.findAll();
        return R.ok(productVos);
    }

    /**
     * 注册设备
     */
    @PostMapping("/register")
    @ApiOperation(value = "注册设备")
    public AjaxResult registerDevice(@RequestBody DeviceDto dto) {
        deviceService.registerDevice(dto);
        return success();
    }

    /**
     * 查询设备详细数据
     */
    @GetMapping("/{iotId}")
    @ApiOperation("获取设备详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iotId", value = "物联网设备id", required = true, dataTypeClass = String.class)
    })
    public R<DeviceDetailVo> getInfo(@PathVariable String iotId) {
        DeviceDetailVo deviceDetailVo = deviceService.queryDeviceDetail(iotId);
        return R.ok(deviceDetailVo);
    }

    /**
     * 查看设备上报的数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查询设备上报数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iotId", value = "物联网设备id", required = true, dataTypeClass = String.class)
    })
    public R<List<DeviceShadowDataVo>> queryServiceProperties(@PathVariable String iotId) {
        List<DeviceShadowDataVo> deviceShadowDataVos = deviceService.queryServiceProperties(iotId);
        return R.ok(deviceShadowDataVos);
    }

    /**
     * 修改设备
     */
    @PutMapping
    @ApiOperation("修改设备")
    public R updateDeviceByIotId(
            @ApiParam(value = "修改的设备数据") @RequestBody DeviceDto dto) {
        deviceService.updateDeviceByIotId(dto);
        return R.ok();
    }

    /**
     * 删除设备
     */
    @DeleteMapping("/{iotId}")
    @ApiOperation("删除设备")
    public R deleteDeviceByIotId(
            @ApiParam(value = "要删除设备的iotId") @PathVariable String iotId) {
        deviceService.deleteDeviceByIotId(iotId);
        return R.ok();
    }
}
